import cv2
import numpy as np
from schedule import get_path, get_time, get_fileName


class Camera(object):
    """ 通过opencv读取摄像头"""

    def __init__(self):
        self.cap = cv2.VideoCapture(0)
        self.fourcc = cv2.VideoWriter_fourcc(*'H264')  # MJPG
        self.path = get_path()
        self.fileName = get_fileName()
        self.out = cv2.VideoWriter(self.path, self.fourcc, 20.0, (640, 480), True)
        self.time = get_time()

    def __del__(self):
        self.cap.release()

    def get_frame(self):
        flag, frame = self.cap.read()

        assert flag
        flag, jpg = cv2.imencode('.jpg', frame)
        assert flag
        self.out.write(frame)

        return np.array(jpg).tostring()
